package cn.eoeit.mmy.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.eoeit.mmy.bean.Pagination;
import cn.eoeit.mmy.bean.RespCode;
import cn.eoeit.mmy.bean.Response;
import cn.eoeit.mmy.model.DesignInfo;
import cn.eoeit.mmy.model.DeviceInfo;
import cn.eoeit.mmy.model.Factory;
import cn.eoeit.mmy.model.GongXuModel;
import cn.eoeit.mmy.model.LinJianFactory;
import cn.eoeit.mmy.model.LinJianKeyInfo;
import cn.eoeit.mmy.model.LingJianGongXuModel;
import cn.eoeit.mmy.model.LingJianModel;
import cn.eoeit.mmy.model.LinjianSellInfo;
import cn.eoeit.mmy.model.Seller;
import cn.eoeit.mmy.model.UserInfo;
import cn.eoeit.mmy.service.ISystemService;
import cn.eoeit.mmy.service.IUserService;
import cn.eoeit.mmy.service.IlingJianService;

@Controller
@RequestMapping("/lingJian")
public class LingjianController {										

	@Autowired
	private IlingJianService lingJianService;
	
	@Autowired
	private ISystemService systemService;
	
	@Autowired
	private IUserService userService;
	
	
	@RequestMapping("/lingJianList.action")
	public String lingJianList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> userInfos = lingJianService.selectAllLingJians(page, 0);
		modelMap.addAttribute("userInfos", userInfos);
		return "lingJianList";
	}

	@RequestMapping("/lingJianToAdd.action")
	public String lingJianToAdd(ModelMap modelMap) {

		
		return "lingJianToAdd";
	}
	
	
	@RequestMapping("/zhiLiangToAdd.action")
	public String zhiLiangToAdd(ModelMap modelMap) {

		
		return "zhiLiangToAdd";
	}
	
	
	
	

	@RequestMapping("/addLingJianKeyInfo.action")
	public  @ResponseBody Response addLingJianKeyInfo(@RequestBody LinJianKeyInfo user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =lingJianService.finishAddLingJianKeyInfo(user);
		if(user!=null){
			response.setMessage("零件添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("零件信息添加失败"); 
			
		} 
		
		return response;
	}
	
	
	@RequestMapping("/zhiLiangKeyList.action")
	public String zhiLiangKeyList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> userInfos = lingJianService.selectAllKeyInfos(page, 0);
		modelMap.addAttribute("userInfos", userInfos);
		return "zhiLiangKeyList";
	}
	
	@RequestMapping("/lingJianInfo.action")
	public String lingJianInfo(Integer uid,ModelMap modelMap) {

		lingJianService.findLingJianInfo(uid,modelMap);
		return "lingJianInfo";
	}
	
	
	@RequestMapping("/deleteLingJian.action")
	public  @ResponseBody Response deleteLingJian(Integer uid,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		LingJianModel user =lingJianService.finishDeleteLingJian(uid);
		if(user!=null){
			response.setMessage("零件信息删除成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("零件信息失败"); 
			
		} 
		
		return response;
	}
	
	
	@RequestMapping("/findLingJianInfo.action")
	public  @ResponseBody Response findLingJianInfo(String bianHao,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		LingJianModel user =lingJianService.finishLingJianByBianHao(bianHao);
		if(user!=null){
			response.setResult(user);
			response.setMessage("查找零件成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("查找零件失败"); 
			
		} 
		
		return response;
	}
	
	

	
	@RequestMapping("/addLingJian.action")
	public  @ResponseBody Response addLingJian(@RequestBody LingJianModel user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =lingJianService.finishAddLingJian(user);
		if(user!=null){
			response.setMessage("零件添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("零件信息添加失败"); 
			
		} 
		
		return response;
	}
	
	@RequestMapping("/gongXuList.action")
	public String gongXuList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> gongxus = lingJianService.selectAllGongXuModels(page, 0);
		modelMap.addAttribute("gongxus", gongxus);
		return "GongXuList";
	}

	@RequestMapping("/gongXuToAdd.action")
	public String gongXuToAdd(ModelMap modelMap) {

		List<DeviceInfo> deviceInfos=systemService.getDeviceInfos();
		modelMap.put("devices",deviceInfos);
		return "GongXuToAdd";
	}
	
	
	@RequestMapping("/deleteGongXu.action")
	public  @ResponseBody Response deleteGongXu(Integer uid,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		LingJianModel user =lingJianService.finishDeleteLingJian(uid);
		if(user!=null){
			response.setMessage("零件信息删除成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("零件信息失败"); 
			
		} 
		
		return response;
	}
	
	
	@RequestMapping("/addGongXu.action")
	public  @ResponseBody Response addGongXu(@RequestBody GongXuModel user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =lingJianService.finishAddGongXu(user);
		if(user!=null){
			response.setMessage("工序信息信息添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("工序信息添加失败"); 
			
		} 
		
		return response;
	}
	@RequestMapping("/jiaGongInfoToAdd.action")
	public String jiaGongInfoToAdd(Integer uid,ModelMap modelMap) {
		LingJianModel lingJian=lingJianService.findLingJian(uid);
		List<GongXuModel> gongXus=lingJianService.getGongXuModels();
		List<UserInfo> userInfos=userService.getAllJianYanYuan();
		modelMap.put("lingJian",lingJian);
		modelMap.put("gongXus",gongXus);
		modelMap.put("jiaYan",userInfos);
		return "jiaInfoToAdd";
	}
	

	@RequestMapping("/addJiaGongInfo.action")
	public  @ResponseBody Response addJiaGongInfo(@RequestBody LingJianGongXuModel user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =lingJianService.finishAddLingJianGongXu(user);
		if(user!=null){
			response.setMessage("零件工序信息信息添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("零件工序添加失败"); 
			
		} 
		
		return response;
	}
	
	@RequestMapping("/designInfoToAdd.action")
	public String designInfoToAdd(Integer uid,ModelMap modelMap) {
		LingJianModel lingJian=lingJianService.findLingJian(uid);
		List<Factory> gongXus=systemService.getFactorys();
		
		modelMap.put("lingJian",lingJian);
		modelMap.put("factory",gongXus);
	
		return "designInfoToAdd";
	}
	
	

	@RequestMapping("/addDesignInfo.action")
	public  @ResponseBody Response addDesignInfo(@RequestBody DesignInfo user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =lingJianService.finishAddDesignInfo(user);
		if(user!=null){
			response.setMessage("设计信息添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("采购信息添加失败"); 
			
		} 
		
		return response;
	}
	
	
	@RequestMapping("/sellInfoToAdd.action")
	public String sellInfoToAdd(Integer uid,ModelMap modelMap) {
		LingJianModel lingJian=lingJianService.findLingJian(uid);
		List<Seller> sellers=systemService.getSellers();
		List<UserInfo> userInfos=userService.getAllKeHus();
		modelMap.put("lingJian",lingJian);
		modelMap.put("Seller",sellers);
		modelMap.put("customers",userInfos);
		return "sellInfoToAdd";
	}
	
	

	@RequestMapping("/addSellInfo.action")
	public  @ResponseBody Response addSellInfo(@RequestBody LinjianSellInfo user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =lingJianService.finishAddSellInfo(user);
		if(user!=null){
			response.setMessage("销售信息添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("销售信息添加失败"); 
			
		} 
		
		return response;
	}
	
	
	@RequestMapping("/caiGouInfoToAdd.action")
	public String caiGouInfoToAdd(Integer uid,ModelMap modelMap) {
		LingJianModel lingJian=lingJianService.findLingJian(uid);
		List<Factory> gongXus=systemService.getFactorys();
		
		modelMap.put("lingJian",lingJian);
		modelMap.put("factory",gongXus);
		
		return "caigouInfoToAdd";
	}
	

	@RequestMapping("/addCaigouInfo.action")
	public  @ResponseBody Response addCaigouInfo(@RequestBody LinJianFactory user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =lingJianService.finishAddCaigouInfo(user);
		if(user!=null){
			response.setMessage("采购信息添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("采购信息添加失败"); 
			
		} 
		
		return response;
	}
}
